#include "HX711.h" //You must have this library in your arduino library folder // LOADSENSOR SETT UP #define DOUT 3 #define CLK 2 #define DOUT1 7 #define CLK1 8 HX711 scale(DOUT, CLK); HX711 scale1(DOUT1, CLK1); //Change this calibration factor as per your load cell once it is found you many need to vary it in thousands float calibration_factor = -96650; //-106600 worked for my 40Kg max scale setup // PS2_MMOUSE #define MDATA 5 #define MCLK 6 #include char mstat; char mx; char my; void gohi(int pin) { pinMode(pin, INPUT); digitalWrite(pin, HIGH); } void golo(int pin) { pinMode(pin,OUTPUT); digitalWrite(pin, LOW); } void mouse_write(char data) { char i; char parity = 1; /*Serial.print("Sending "); Serial.print(data, HEX); Serial.print(" to mouse\n"); Serial.print("RTS"); /* put pins in output mode */ gohi(MDATA); gohi(MCLK); delayMicroseconds(300); golo(MCLK); delayMicroseconds(300); golo(MDATA); delayMicroseconds(10); /* start bit */ gohi(MCLK); /* wait for mouse to take control of clock); */ while (digitalRead(MCLK) == HIGH) ; /* clock is low, and we are clear to send data */ for (i=0; i < 8; i++) { if (data & 0x01) { gohi(MDATA); } else { golo(MDATA); } /* wait for clock cycle */ while (digitalRead(MCLK) == LOW) ; while (digitalRead(MCLK) == HIGH) ; parity = parity ^ (data & 0x01); data = data >> 1; } /* parity */ if (parity) { gohi(MDATA); } else { golo(MDATA); } while (digitalRead(MCLK) == LOW) ; while (digitalRead(MCLK) == HIGH) ; /* stop bit */ gohi(MDATA); delayMicroseconds(50); while (digitalRead(MCLK) == HIGH) ; /* wait for mouse to switch modes */ while ((digitalRead(MCLK) == LOW) || (digitalRead(MDATA) == LOW)) ; /* put a hold on the incoming data. */ golo(MCLK); /*Serial.print("done.\n");*/ } /* * Get a byte of data from the mouse */ char mouse_read(void) { char data = 0x00; int i; char bit = 0x01; //Serial.print("reading byte from mouse\n"); /* start the clock */ gohi(MCLK); gohi(MDATA); delayMicroseconds(50); while (digitalRead(MCLK) == HIGH) ; delayMicroseconds(5); /* not sure why */ while (digitalRead(MCLK) == LOW) /* eat start bit */ ; for (i=0; i < 8; i++) { while (digitalRead(MCLK) == HIGH) ; if (digitalRead(MDATA) == HIGH) { data = data | bit; } while (digitalRead(MCLK) == LOW) ; bit = bit << 1; } /* eat parity bit, which we ignore */ while (digitalRead(MCLK) == HIGH) ; while (digitalRead(MCLK) == LOW) ; /* eat stop bit */ while (digitalRead(MCLK) == HIGH) ; while (digitalRead(MCLK) == LOW) ; /* put a hold on the incoming data. */ golo(MCLK); // Serial.print("Recvd data "); //Serial.print(data, HEX); // Serial.print(" from mouse\n"); return data; } void mouse_init() { gohi(MCLK); gohi(MDATA); //Serial.print("Sending reset to mouse\n"); mouse_write(0xff); mouse_read(); /* ack byte */ // Serial.print("Read ack byte1\n"); mouse_read(); /* blank */ mouse_read(); /* blank */ //Serial.print("Sending remote mode code\n"); mouse_write(0xf0); /* remote mode */ mouse_read(); /* ack */ // Serial.print("Read ack byte2\n"); delayMicroseconds(100); } void setup() { Serial.begin(9600); mouse_init(); /* Serial.println("HX711 Calibration"); Serial.println("Remove all weight from scale"); Serial.println("After readings begin, place known weight on scale"); Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively"); Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively"); Serial.println("Press t for tare");*/ scale.set_scale(); scale1.set_scale(); scale.tare(); //Reset the scale to 0 scale1.tare(); Serial.println("CLEARDATA"); Serial.print("LABEL,FX, FY ,mx, my"); Serial.println("RESETTIMER"); long zero_factor = scale.read_average();//Get a baseline reading long zero_factor1 = scale1.read_average(); Serial.print("Zero factor: ");//This can be used to remove the need to tare the scale. Useful in permanent scale projects. Serial.println(zero_factor); Serial.print("Zero factor1: "); Serial.println(zero_factor1); } //============================================================================================= // LOOP //============================================================================================= void loop() { Serial.print("DATA,-scale.get_units, -scale1.get_units, mx, my"); scale.set_scale(calibration_factor); //Adjust to this calibration factor scale1.set_scale(calibration_factor); //MOUSE mouse_write(0xeb); /* give me data! */ mouse_read(); /* ignore ack */ mstat = mouse_read(); mx = mouse_read(); my = mouse_read(); // Serial.print("Reading: "); Serial.print(","); Serial.print(-scale.get_units(), 3); // Serial.print(" kg"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person //Serial.print(" calibration_factor: "); //Serial.print(calibration_factor); Serial.print(","); // Serial.print("Reading1: "); Serial.print(-scale1.get_units(), 3); //Serial.print(" kg"); Serial.print(","); //Serial.println(); /* send the data back up */ //Serial.print(mstat, BIN); // Serial.print("\tX="); Serial.print(mx, DEC); Serial.print(" , "); //Serial.print("\tY="); Serial.print(my, DEC); Serial.print(" , "); Serial.println(); delay(500); if(Serial.available()) { char temp = Serial.read(); if(temp == '+' || temp == 'a') calibration_factor += 10; else if(temp == '-' || temp == 'z') calibration_factor -= 10; else if(temp == 's') calibration_factor += 100; else if(temp == 'x') calibration_factor -= 100; else if(temp == 'd') calibration_factor += 1000; else if(temp == 'c') calibration_factor -= 1000; else if(temp == 'f') calibration_factor += 10000; else if(temp == 'v') calibration_factor -= 10000; else if(temp == 't') scale.tare(); //Reset the scale to zero delay(500); } }